#!/bin/sh
# the next line restarts using tclsh \
exec wish "$0" "$@"

###############################################################################
##                                                                           ##
##  Copyright (c) 2006-2008, Gerald W. Lester                                ##
##                                                                           ##
##  All rights reserved.                                                     ##
##                                                                           ##
##  Redistribution and use in source and binary forms, with or without       ##
##  modification, are permitted provided that the following conditions       ##
##  are met:                                                                 ##
##                                                                           ##
##    * Redistributions of source code must retain the above copyright       ##
##      notice, this list of conditions and the following disclaimer.        ##
##    * Redistributions in binary form must reproduce the above              ##
##      copyright notice, this list of conditions and the following          ##
##      disclaimer in the documentation and/or other materials provided      ##
##      with the distribution.                                               ##
##    * Neither the name of the KnG Consulting, LLC nor the names            ##
##      of its contributors may be used to endorse or promote products       ##
##      derived from this software without specific prior written            ##
##      permission.                                                          ##
##                                                                           ##
##  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS      ##
##  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT        ##
##  LIMITED  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS       ##
##  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE           ##
##  COPYRIGHT OWNER OR  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,     ##
##  INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,    ##
##  BUT NOT LIMITED TO,  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;        ##
##  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER         ##
##  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT       ##
##  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR  OTHERWISE) ARISING IN       ##
##  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF  ADVISED OF THE         ##
##  POSSIBILITY OF SUCH DAMAGE.                                              ##
##                                                                           ##
###############################################################################

##
## Add application specific libraries to the Auto Path
##
lappend auto_path [file join [file dirname [info script]] libs]

##
## Load the packages we need
##
package require Tcl 8.5
package require Tk
package require inifile
package require FreshBooksApi
package require msgcat

##
## Get the directories where we store files
##
set scriptDir [file normalize [file dirname [info script]]]
set execDir [file normalize [file dirname [info nameofexecutable]]]
set pwd [file normalize [pwd]]
set home [file normalize ~/]

##
## Set the list of pieces of the API
##
set apiList {
    Callback
    Category
    Client
    Estimate
    Expense
    Invoice
    Item
    Payment
    Project
    Recurring
    Staff
    System
    Task
    TimeEntry
}

##
## Load in the message catalogs for the local
##
set dirList [list \
    $scriptDir \
    [file join $execDir FreshBooksExplorer] \
    [file join $pwd FreshBooksExplorer] \
    [file join $home FreshBooksExplorer] \
]
foreach dir $dirList {
    catch {::msgcat::mcload [file join $dir msgcat]}
}

##
## Load in the user initialization file
##
set dirList [list \
    $home \
    $pwd \
    $scriptDir \
]
set found 0
foreach dir $dirList {
    set fn [file join $dir FreshBooksExplorer.ini]
    if {[file exists $fn]} {
        set found 1
        break
    }
}

##
## Load in the sources for the GUI
##
foreach file [glob [file join $scriptDir Gui *.tcl]] {
    source $file
}

##
## Load in the sources that populate the FreshBook screens
##
foreach file [glob [file join $scriptDir FbRoutines *.tcl]] {
    source $file
}

##
## Create all the GUI
##
CreateGui

##
## Load the initialization
##
if {!$found} {
    ##
    ## If no init file found, display modal dialog to get required information
    ##
    FirstTimeInitialization
} else {
    ##
    ## Open the file and get the information we need
    ##
    set id [::ini::open $fn r]
    set configuration(Connection,Url) [::ini::value $id Connection URL]
    set configuration(Connection,Token) [::ini::value $id Connection AuthenticationToken]
    ::ini::close $id
}

#::log::lvSuppressLE debug 0

##
## Connect to FreshBooks
##
::FreshBooksApi::Initialize $configuration(Connection,Url) $configuration(Connection,Token)

##
## Cause the System tab of the main window to be displayed.
##
.body.nb tab .body.nb.system -state normal
.body.nb select .body.nb.system
